<?php
/**
 * Description: 门店主页
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/5/19
 * Time: 15:34
 */

use Biz\model\store\MStoreContent;
use Biz\model\store\StoreBaseDecorationModel;
use Biz\model\store\StoreCaseModel;
use Biz\model\store\StoreImagesModel;
use Biz\model\store\StoreOtherStoresModel;
use Biz\model\store\StoreSaleModel;
use Biz\model\store\StoreVideoModel;
use Biz\service\Store\CustomCategoryWikiService;
use Biz\service\Store\StoreAskService;
use Biz\service\Store\StoreBasicService;
use Biz\service\Store\StoreHomeService;
use Doraemon\pockets\constInterface\statSign\AutoModule;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\Hhzurl;


class HomeAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws Exception
     */
    public function main()
    {
        $store_uid = $this->getRequest()->getPost('uid') ?? 0;
        $uid = $this->uid;
        $data = $this->_getBasicStoreInfo($store_uid, $uid);
        if (!$store_uid || !$data) {
            throw new \Exception("门店不存在！");
        }
        $store_id = $data['id'];
        $data['show_mini_program'] = 1;//分享展示
        //更多门店link
        $data['more_store_link'] = 'niceliving://webview/normalscreen?protego=' . urlencode(base64_encode(json_encode(['url' => Hhzurl::getDomain() . '/store/store-list?store_id=' . $store_id])));
        //门店简介link
        $data['store_intro_link'] = 'niceliving://webview/normalscreen?protego=' . urlencode(base64_encode(json_encode(['url' => Hhzurl::getDomain() . '/store/store-intro?store_id=' . $store_id])));
        $url = Hhzurl::getDomain() . '/community/map?longitude=' . $data['longitude'] . '&latitude=' . $data['latitude'] . "&shop_name=" . urlencode($data['store_name']) . "&address=" . urlencode($data['show_address']) . "&hhzjump=1";
        $data['map_link'] = 'niceliving://webview/normalscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url])));
        $data['video_total'] = StoreVideoModel::getPassTotal($store_id);
        $data['img_total'] = StoreImagesModel::getPassTotal($store_id);
        $data['sale_info'] = $this->_getSaleInfo($store_id);
        $data['other_stores_total'] = StoreOtherStoresModel::getCount(['check_status' => StoreOtherStoresModel::NORMAL, 'store_id' => $store_id]);
        $data['share_info'] = \Biz\service\Share::getStoreGloryShare($data);
        $ask_status = StoreHomeService::getIssueStatus($store_id);
        $data['list'] = [
            [
                'type' => BlockType::STORE_WIKI_INFO,
                'wiki_info' => $this->_getWikiInfo($store_id)
            ],
            [
                'type' => BlockType::STORE_CASE_INFO,
                'case_info' => $this->_getCaseInfo($store_id)
            ],
        ];

        if ((int)$ask_status !== StoreBaseDecorationModel::OFF) {
            $ask_arr = [
                'type' => BlockType::STORE_ASK_INFO,
                'ask_info' => $this->_getAskInfo($store_id)
            ];
            $data['list'] = array_merge($data['list'], [$ask_arr]);
        }

        $log_data['uid'] = $uid;
        $log_data['store_id'] = $store_id;
        ActionLog::log($uid, $log_data, ActionLog::STORE_HOMEPAGE_VISIT);
        $this->setSuccess($data);

    }

    /**
     * 获取优惠信息
     * @param $id
     * @return array
     */
    private function _getSaleInfo($id): array
    {
        $where['content[!]'] = '';
        $field = ['content', 'sale_type'];
        $list = StoreHomeService::getSaleList($id, $where, $field) ?: [];
        $data['total'] = count($list);
        $content = array_column($list, 'content', 'sale_type');
//        $data['content'] = $content[StoreSaleModel::STORE_SALE_TYPE] ?: $content[StoreSaleModel::IN_STORE_TYPE];
        $data['content'] = $content[StoreSaleModel::STORE_SALE_TYPE] ?: "";
        return $data;
    }

    /**
     * 获取门店基础信息
     * @param $store_uid
     * @param $uid
     * @return array
     */
    private function _getBasicStoreInfo($store_uid, $uid): array
    {
        return StoreBasicService::getOneStoreInfoByUid($store_uid, $uid);
    }

    /**
     * 获取热门商品
     * @param $store_id
     * @return array
     * @throws Exception
     */
    private function _getWikiInfo($store_id): array
    {
        $result = [];
        $data = CustomCategoryWikiService::getWikiByStoreId($store_id, 0, 4);
        if ($data['list']) {
            foreach ($data['list'] as &$item) {
                $item['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_STORE_HOME_WIKI_LIST, [],
                    ['wiki_id' => $item['id'], 'store_id' => $store_id, 'act_from' => 'store_home']);
            }
        }
        $data['list']= \Doraemon\tools\StatSign::versionCompatible($data['list'], \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, \Doraemon\tools\BlockType::TYPE_WIKI, 'wiki');
        return $data;

    }

    /**
     * 获取案例信息
     * @param $store_id
     * @return array
     */
    private function _getCaseInfo($store_id): array
    {
        $field = ['store_id', 'case_id', 'house_area', 'house_price'];
        $data['total'] = StoreCaseModel::getPassCount($store_id) ?: 0;
        $where_ext['LIMIT'] = 3;
        $where_ext['check_status'] = StoreCaseModel::PASS;
        $list = StoreHomeService::getCaseList($store_id, $field, $where_ext);
        foreach ($list as &$item) {
            $obj_type = \Doraemon\tools\IdParse::getType($item['case_id']);
            $item['statSign'] = basicStatSign::genStatSign(AutoModule::MODULE_STORE_HOME_CASE_LIST, [basicStatSign::EXTEND_KEY_OBJ_ID => $item['case_id'], basicStatSign::EXTEND_KEY_OBJ_TYPE => $obj_type ?? null],
                ['store_id' => $store_id, 'act_from' => 'store_home']);
        }
        $list= \Doraemon\tools\StatSign::versionCompatible($list, \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, \Doraemon\tools\BlockType::TYPE_STORE_CASE, 'sub_case');
        $data['list'] = $list;
        return $data;
    }

    /**
     * 获取问答列表
     * @param $store_id
     * @return array
     */
    private function _getAskInfo($store_id)
    {
        $field = ['content', 'id'];
        $list = StoreAskService::getAskList($store_id, $field);

        if (!empty($list['list'])) {
            $list['list'] = array_map(function ($v) {
                return [
                    'question_id' => $v['id'],
                    'question' => $v['content'],
                ];
            }, $list['list']);
        }
        return $list;
    }

}